<template>
	<div>
		<el-card class="!border-none mb-4" shadow="never">
			<el-form class="mb-[-16px]" :model="queryParams" inline>
				<el-form-item label="类型" prop="type">
					<el-input class="w-[280px]" v-model="queryParams.type" clearable placeholder="请输入类型" />
				</el-form-item>
				<el-form-item label="名称" prop="name">
					<el-input class="w-[280px]" v-model="queryParams.name" clearable placeholder="请输入名称" />
				</el-form-item>
				<el-form-item label="值" prop="value">
					<el-input class="w-[280px]" v-model="queryParams.value" clearable placeholder="请输入值" />
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="resetPage">查询</el-button>
					<!-- <el-button @click="resetParams">重置</el-button> -->
				</el-form-item>
			</el-form>
		</el-card>
		<el-card class="!border-none" v-loading="pager.loading" shadow="never">
			<el-tabs v-model="langs.symbol" @tab-change="handChange">
				<el-tab-pane v-for="(item, index) in langs.list" :key="index" :label="item.name" :name="item.symbol"
					lazy></el-tab-pane>
			</el-tabs>
			<div class="flex justify-start">
				<el-button v-perms="['setting.language_pag/add']" type="primary" @click="handleAdd" v-if="active==0">
					<template #icon>
						<icon name="el-icon-Plus" />
					</template>
					新增
				</el-button>
				<el-button v-perms="['setting.language_pag/sync']" @click="handleSync" v-if="active!=0">
					同步
				</el-button>
				<el-button v-perms="['setting.language_pag/trans']" @click="handleTrans" v-if="active!=0">
					翻译
				</el-button>
			</div>
			<div class="mt-4">
				<el-table :data="pager.lists">
					<el-table-column label="类型" prop="type" show-overflow-tooltip />
					<el-table-column label="名称" prop="name" show-overflow-tooltip />
					<el-table-column label="UNIAPP引用" min-width="160" show-overflow-tooltip>
						<template #default="{ row }">
							<div>t('{{row.type}}.{{row.name}}')</div>
						</template>
					</el-table-column>
					<el-table-column label="源语言" prop="from_value" show-overflow-tooltip v-if="active!=0" />
					<el-table-column label="值" prop="value" show-overflow-tooltip />
					<el-table-column label="操作" width="120" fixed="right">
						<template #default="{ row }">
							<el-button v-perms="['setting.language_pag/edit']" type="primary" link
								@click="handleEdit(row)">
								编辑
							</el-button>
							<el-button v-perms="['setting.language_pag/delete']" type="danger" link
								@click="handleDelete(row.id)">
								删除
							</el-button>
						</template>
					</el-table-column>
				</el-table>
			</div>
			<div class="flex mt-4 justify-end">
				<pagination v-model="pager" @change="getLists" />
			</div>
		</el-card>
		<edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
	</div>
</template>

<script lang="ts" setup name="languagePagLists">
	import { usePaging } from '@/hooks/usePaging'
	import { apiLanguagePagLists, apiLanguagePagDelete, apiLanguagePagSync, apiLanguagePagTrans } from '@/api/setting/language_pag'
	import { apiLanguageAll } from '@/api/setting/language'
	import feedback from '@/utils/feedback'
	import EditPopup from './edit.vue'

// 需要使用替换的语言包类型：activity、captcha、item、mission、recharge、team、upload （自动翻译后请注意检查，防止翻译错误）

	const editRef = shallowRef<InstanceType<typeof EditPopup>>()
	// 是否显示编辑框
	const showEdit = ref(false)
	const active = ref(0)

	const langs = reactive({
		list: [{
			name: '',
			symbol: '',
			id: ''
		}],
		symbol: ''
	})

	// 查询条件
	const queryParams = reactive({
		type: '',
		name: '',
		value: '',
		lang: langs.symbol,
	})

	// 选中数据
	const selectData = ref<any[]>([])


	// 表格选择后回调事件
	const handleSelectionChange = (val : any[]) => {
		selectData.value = val.map(({ id }) => id)
	}

	// 分页相关
	const { pager, getLists, resetParams, resetPage } = usePaging({
		fetchFun: apiLanguagePagLists,
		params: queryParams
	})

	// 添加
	const handleAdd = async () => {
		showEdit.value = true
		await nextTick()
		editRef.value?.open('add')
		editRef.value?.setFormData({ 'lang': langs.symbol })
		editRef.value?.setFormData({ 'type': queryParams.type })
	}

	// 编辑
	const handleEdit = async (data : any) => {
		showEdit.value = true
		await nextTick()
		editRef.value?.open('edit')
		editRef.value?.setFormData(data)
	}

	// 删除
	const handleDelete = async (id : number | any[]) => {
		await feedback.confirm('确定要删除？')
		await apiLanguagePagDelete({ id })
		getLists()
	}
	// 同步
	const handleSync = async () => {
		await feedback.confirm('当前语言包同步首个语言，确定要同步？')
		await apiLanguagePagSync({ from_id: langs['list'][0]['id'], to_id: langs['list'][active.value]['id'] })
		getLists()
	}
	// 翻译
	const handleTrans = async () => {
		await feedback.confirm('翻译未填写的值，确定要翻译？')
		await apiLanguagePagTrans({ from_id: langs['list'][0]['id'], to_id: langs['list'][active.value]['id'] })
		getLists()
	}

	const getData = async () => {
		const data = await apiLanguageAll()
		langs.list = data;
		langs.symbol = data[0]['symbol'];
		queryParams.lang = langs.symbol;
	}
	//标签切换
	const handChange = async (data : any) => {
		active.value = langs.list.findIndex((item) => item.symbol === data);

		queryParams.lang = data
		getLists()
	}

	getData()
</script>